package com.example.j202411.views.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.j202411.Model.BusResponse.BusInfo;
import com.example.j202411.R;
import com.example.j202411.viewmodel.BusViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BusLineActivity extends AppCompatActivity {

    private Spinner spinnerCity, spinnerLine;
    private Button buttonQuery;
    private ListView listView;
    private BusViewModel busViewModel;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_businfo);

            spinnerCity = findViewById(R.id.spinner_city);
            spinnerLine = findViewById(R.id.spinner_line);
            buttonQuery = findViewById(R.id.button_query);
            listView = findViewById(R.id.listView);

            busViewModel = new ViewModelProvider(this).get(BusViewModel.class);

            // 设置城市选择器的数据
            String[] cities = getResources().getStringArray(R.array.cities);
            ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, cities);
            cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerCity.setAdapter(cityAdapter);

            // 设置线路选择器的数据（这里假设线路数据是动态的）
            spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String selectedCity = (String) parent.getSelectedItem();
                    setupLinesForCity(selectedCity);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    // Do nothing
                }
            });

            // 查询按钮点击事件
            buttonQuery.setOnClickListener(v -> {
                String selectedCity = spinnerCity.getSelectedItem().toString();
                String selectedLine = spinnerLine.getSelectedItem().toString();
                if (!selectedCity.isEmpty() && !selectedLine.isEmpty()) {
                    busViewModel.getBusInfo(selectedCity, selectedLine, "").observe(BusLineActivity.this, busInfos -> {
                        if (busInfos != null) {
                            List<Map<String, String>> data = new ArrayList<>();
                            for (BusInfo info : busInfos) {
                                Map<String, String> map = new HashMap<>();
                                map.put("lines", "线路: " + info.getLines());
                                map.put("price", "票价: " + info.getPrice());
                                map.put("endSn", "终点站: " + info.getEndSn());
                                map.put("busId", "公交车ID: " + info.getBusId());
                                map.put("reachtime", "到达时间: " + info.getReachtime());
                                map.put("travelTime", "行驶时间: " + info.getTravelTime());
                                map.put("surplus", "剩余站数: " + info.getSurplus());
                                data.add(map);
                            }

                            SimpleAdapter adapter = new SimpleAdapter(BusLineActivity.this, data,
                                    R.layout.list_item_bus_info,
                                    new String[]{"lines", "price", "endSn", "busId", "reachtime", "travelTime", "surplus"},
                                    new int[]{R.id.text_lines, R.id.text_price, R.id.text_endSn, R.id.text_busId, R.id.text_reachtime, R.id.text_travelTime, R.id.text_surplus});
                            listView.setAdapter(adapter);
                        } else {
                            Toast.makeText(BusLineActivity.this, "没有找到相关数据", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    Toast.makeText(BusLineActivity.this, "请选择城市和线路", Toast.LENGTH_SHORT).show();
                }
            });
        }

        private void setupLinesForCity(String city) {
            // 这里可以动态加载线路数据，或者预先定义好
            // 示例中使用静态线路数据
            String[] lines = {"深圳石油大厦", "其他线路"};
            if ("北京".equals(city)) {
                lines = new String[]{"天安门", "西单", "东直门"};
            } else if ("上海".equals(city)) {
                lines = new String[]{"外滩", "陆家嘴", "静安寺"};
            } else if ("广州".equals(city)) {
                lines = new String[]{"天河城", "北京路", "上下九"};
            } else if ("深圳".equals(city)) {
                lines = new String[]{"深圳石油大厦", "罗湖口岸", "福田中心"};
            }

            ArrayAdapter<String> lineAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, lines);
            lineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerLine.setAdapter(lineAdapter);
        }
}